<template>
	<gradient-background>
		<view class="container">
			<view class="content">
				<!-- 页面标题 -->
				<view class="page-header">
					<view class="back-button" @tap="goBack">
						<text class="back-icon">←</text>
					</view>
					<view class="header-info">
						<text class="tool-icon">💕</text>
						<text class="tool-title">爱情占卜</text>
						<text class="tool-subtitle">感情运势和桃花分析</text>
					</view>
				</view>
				
				<!-- 参数输入区域 -->
				<app-card>
					<view class="input-section">
						<text class="section-title">基本信息</text>
						
						<view class="form-item">
							<text class="form-label">性别</text>
							<view class="gender-selector">
								<view 
									class="gender-item"
									:class="{ active: params.gender === 'male' }"
									@tap="selectGender('male')"
								>
									<text class="gender-icon">👨</text>
									<text class="gender-text">男</text>
								</view>
								<view 
									class="gender-item"
									:class="{ active: params.gender === 'female' }"
									@tap="selectGender('female')"
								>
									<text class="gender-icon">👩</text>
									<text class="gender-text">女</text>
								</view>
							</view>
						</view>
						
						<view class="form-item">
							<text class="form-label">感情状态</text>
							<picker 
								mode="selector" 
								:range="statusOptions" 
								:value="statusIndex"
								@change="onStatusChange"
								class="status-picker"
							>
								<view class="picker-display">
									<text class="picker-text">{{ displayStatus }}</text>
									<text class="picker-arrow">→</text>
								</view>
							</picker>
						</view>
						
						<view class="form-item">
							<text class="form-label">生日</text>
							<picker 
								mode="date" 
								:value="params.birthday" 
								@change="onBirthdayChange"
								class="birthday-picker"
							>
								<view class="picker-display">
									<text class="picker-text">{{ displayBirthday }}</text>
									<text class="picker-arrow">→</text>
								</view>
							</picker>
						</view>
						
						<view class="form-item">
							<text class="form-label">关注重点</text>
							<view class="focus-selector">
								<view 
									v-for="focus in focusAreas" 
									:key="focus.value"
									class="focus-item"
									:class="{ active: params.focus === focus.value }"
									@tap="selectFocus(focus.value)"
								>
									<text class="focus-icon">{{ focus.icon }}</text>
									<text class="focus-text">{{ focus.name }}</text>
								</view>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 查询按钮 -->
				<view class="action-section">
					<app-button 
						:text="loading ? '占卜中...' : '开始占卜'" 
						type="primary"
						@tap="queryLove"
						:disabled="!canQuery || loading"
						:loading="loading"
					></app-button>
				</view>
				
				<!-- 结果显示区域 -->
				<app-card v-if="loveResult">
					<view class="result-section">
						<text class="section-title">爱情运势</text>
						
						<view class="love-analysis">
							<view class="overall-love">
								<view class="love-score">
									<view class="score-circle">
										<text class="score-text">{{ loveResult.overall.score }}</text>
										<text class="score-unit">分</text>
									</view>
									<text class="score-level">{{ loveResult.overall.level }}</text>
								</view>
								<view class="love-desc">
									<text class="desc-text">{{ loveResult.overall.description }}</text>
								</view>
							</view>
							
							<view class="love-details">
								<view 
									v-for="(detail, index) in loveResult.details" 
									:key="index"
									class="detail-item"
								>
									<text class="detail-title">{{ detail.title }}</text>
									<text class="detail-content">{{ detail.content }}</text>
									<view class="detail-score">
										<text class="score-text">{{ detail.score }}分</text>
									</view>
								</view>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 爱情建议 -->
				<app-card v-if="loveResult && loveResult.suggestions">
					<view class="suggestions-section">
						<text class="section-title">爱情建议</text>
						
						<view class="suggestions-list">
							<view 
								v-for="(suggestion, index) in loveResult.suggestions" 
								:key="index"
								class="suggestion-item"
							>
								<view class="suggestion-icon">
									<text class="icon-text">💡</text>
								</view>
								<text class="suggestion-text">{{ suggestion }}</text>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 幸运元素 -->
				<app-card v-if="loveResult && loveResult.luckyElements">
					<view class="lucky-section">
						<text class="section-title">爱情幸运元素</text>
						
						<view class="lucky-elements">
							<view class="element-item">
								<text class="element-label">幸运色</text>
								<text class="element-value">{{ loveResult.luckyElements.color }}</text>
							</view>
							<view class="element-item">
								<text class="element-label">幸运数字</text>
								<text class="element-value">{{ loveResult.luckyElements.number }}</text>
							</view>
							<view class="element-item">
								<text class="element-label">幸运花</text>
								<text class="element-value">{{ loveResult.luckyElements.flower }}</text>
							</view>
							<view class="element-item">
								<text class="element-label">吉利方位</text>
								<text class="element-value">{{ loveResult.luckyElements.direction }}</text>
							</view>
						</view>
					</view>
				</app-card>
			</view>
		</view>
	</gradient-background>
</template>

<script>
import GradientBackground from '@/components/gradient-background/gradient-background.vue'
import AppCard from '@/components/app-card/app-card.vue'
import AppButton from '@/components/app-button/app-button.vue'
import ToolsService from '@/utils/tools-service.js'

export default {
	name: 'LoveToolPage',
	components: {
		GradientBackground,
		AppCard,
		AppButton
	},
	data() {
		return {
			params: {
				gender: '',
				status: '',
				birthday: '',
				focus: ''
			},
			loveResult: null,
			loading: false,
			toolsService: null,
			statusIndex: 0,
			statusOptions: ['单身', '恋爱中', '已婚', '复杂关系'],
			focusAreas: [
				{ value: 'overall', name: '整体运势', icon: '🌟' },
				{ value: 'peach', name: '桃花运', icon: '🌸' },
				{ value: 'existing', name: '现有关系', icon: '💖' },
				{ value: 'future', name: '未来发展', icon: '🔮' }
			]
		}
	},
	computed: {
		canQuery() {
			return this.params.gender && this.params.status && 
				   this.params.birthday && this.params.focus
		},
		
		displayStatus() {
			return this.params.status || '选择感情状态'
		},
		
		displayBirthday() {
			return this.params.birthday || '选择生日'
		}
	},
	onLoad() {
		this.initData()
	},
	methods: {
		initData() {
			this.toolsService = new ToolsService()
		},
		
		goBack() {
			uni.navigateBack()
		},
		
		selectGender(gender) {
			this.params.gender = gender
		},
		
		selectFocus(focus) {
			this.params.focus = focus
		},
		
		onStatusChange(e) {
			this.statusIndex = e.detail.value
			this.params.status = this.statusOptions[e.detail.value]
		},
		
		onBirthdayChange(e) {
			this.params.birthday = e.detail.value
		},
		
		async queryLove() {
			if (!this.canQuery) {
				uni.showToast({
					title: '请完善基本信息',
					icon: 'none'
				})
				return
			}
			
			this.loading = true
			
			try {
				const result = await this.toolsService.getLoveInfo(this.params)
				
				if (result.success) {
					this.loveResult = result.data
					uni.showToast({
						title: '占卜完成',
						icon: 'success'
					})
				} else {
					throw new Error(result.message || '占卜失败')
				}
			} catch (error) {
				console.error('爱情占卜失败:', error)
				uni.showToast({
					title: '占卜失败，请重试',
					icon: 'none'
				})
			} finally {
				this.loading = false
			}
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	min-height: 100vh;
	padding: 32rpx;
}

.content {
	padding-top: 40rpx;
}

.page-header {
	display: flex;
	align-items: center;
	margin-bottom: 40rpx;
	
	.back-button {
		width: 80rpx;
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: rgba(255, 255, 255, 0.9);
		border-radius: 40rpx;
		margin-right: 24rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.1);
	}
	
	.header-info {
		flex: 1;
		
		.tool-icon {
			font-size: 64rpx;
		}
		
		.tool-title {
			display: block;
			font-size: 40rpx;
			font-weight: bold;
			color: #000000;
		}
	}
}

.input-section {
	.form-item {
		margin-bottom: 32rpx;
		
		.form-label {
			display: block;
			font-size: 28rpx;
			color: #000000;
			margin-bottom: 16rpx;
		}
		
		.gender-selector {
			display: flex;
			gap: 16rpx;
			
			.gender-item {
				flex: 1;
				display: flex;
				flex-direction: column;
				align-items: center;
				padding: 24rpx;
				border: 2rpx solid #f0f0f0;
				border-radius: 12rpx;
				transition: all 0.3s ease;
				
				&.active {
					border-color: #FF6B9D;
					background: linear-gradient(135deg, #FF6B9D 0%, #E91E63 100%);
					color: white;
				}
			}
		}
		
		.focus-selector {
			display: grid;
			grid-template-columns: repeat(2, 1fr);
			gap: 12rpx;
			
			.focus-item {
				display: flex;
				flex-direction: column;
				align-items: center;
				padding: 20rpx;
				border: 2rpx solid #f0f0f0;
				border-radius: 12rpx;
				transition: all 0.3s ease;
				
				&.active {
					border-color: #FF6B9D;
					background: linear-gradient(135deg, #FF6B9D 0%, #E91E63 100%);
					color: white;
				}
			}
		}
		
		.picker-display {
			display: flex;
			align-items: center;
			justify-content: space-between;
			height: 88rpx;
			padding: 0 24rpx;
			border: 2rpx solid #f0f0f0;
			border-radius: 12rpx;
			background-color: #fafafa;
		}
	}
}

.action-section {
	margin: 48rpx 0;
}

.love-analysis {
	.overall-love {
		display: flex;
		align-items: center;
		margin-bottom: 32rpx;
		padding: 32rpx;
		background: linear-gradient(135deg, #FF6B9D 0%, #E91E63 100%);
		border-radius: 16rpx;
		
		.love-score {
			display: flex;
			flex-direction: column;
			align-items: center;
			margin-right: 32rpx;
			
			.score-circle {
				width: 120rpx;
				height: 120rpx;
				border: 4rpx solid rgba(255, 255, 255, 0.3);
				border-radius: 50%;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				margin-bottom: 12rpx;
				
				.score-text {
					font-size: 36rpx;
					font-weight: bold;
					color: white;
				}
				
				.score-unit {
					font-size: 20rpx;
					color: rgba(255, 255, 255, 0.8);
				}
			}
			
			.score-level {
				font-size: 24rpx;
				color: white;
				font-weight: 500;
			}
		}
		
		.love-desc {
			flex: 1;
			
			.desc-text {
				font-size: 28rpx;
				color: white;
				line-height: 1.6;
			}
		}
	}
	
	.love-details {
		display: flex;
		flex-direction: column;
		gap: 16rpx;
		
		.detail-item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 24rpx;
			background-color: #f8f9fa;
			border-radius: 12rpx;
			
			.detail-title {
				font-size: 28rpx;
				font-weight: 500;
				color: #000000;
			}
			
			.detail-content {
				flex: 1;
				font-size: 26rpx;
				color: rgba(0, 0, 0, 0.8);
				margin: 0 16rpx;
			}
			
			.detail-score {
				.score-text {
					font-size: 24rpx;
					color: #FF6B9D;
					font-weight: 500;
				}
			}
		}
	}
}

.suggestions-section,
.lucky-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.suggestions-list {
		.suggestion-item {
			display: flex;
			align-items: flex-start;
			margin-bottom: 20rpx;
			
			.suggestion-icon {
				margin-right: 16rpx;
				margin-top: 4rpx;
			}
			
			.suggestion-text {
				flex: 1;
				font-size: 28rpx;
				color: #000000;
				line-height: 1.6;
			}
		}
	}
	
	.lucky-elements {
		display: grid;
		grid-template-columns: repeat(2, 1fr);
		gap: 16rpx;
		
		.element-item {
			display: flex;
			flex-direction: column;
			align-items: center;
			padding: 24rpx;
			background-color: #f8f9fa;
			border-radius: 12rpx;
			
			.element-label {
				font-size: 24rpx;
				color: rgba(0, 0, 0, 0.7);
				margin-bottom: 8rpx;
			}
			
			.element-value {
				font-size: 28rpx;
				font-weight: 500;
				color: #FF6B9D;
			}
		}
	}
}
</style>